package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import com.atguigu.yygu.common.exception.YyghException;
import com.atguigu.yygu.common.resulit.Result;
import com.atguigu.yygu.common.resulit.ResultCodeEnum;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Page;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //删除排班
    @PostMapping("/schedule/remove")
    public Result remove(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1 获取系统中传递过来的MD5加密后的签名
        String hospSign = (String) paramMap.get("sign");
        //2 获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //3 根据医院编码查询数据库获取医院签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //5 判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //排班编号
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
    //查询排班
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1 获取系统中传递过来的MD5加密后的签名
        String hospSign = (String) paramMap.get("sign");
        //2 获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //3 根据医院编码查询数据库获取医院签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //5 判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Integer page = 1;
        Integer limit = 10;
        if (ObjectUtils.isNotEmpty(paramMap.get("page"))) {
            page = Integer.parseInt(paramMap.get("page").toString());
        }
        if (ObjectUtils.isNotEmpty(paramMap.get("limit"))) {
            limit = Integer.parseInt(paramMap.get("limit").toString());
        }

        //科室编号
        String depcode = (String) paramMap.get("depcode");
        ScheduleQueryVo  scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setDepcode(depcode);
        //调用service的方法
        Page<Schedule> page1 = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(page1);
    }

    //上传排班
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1 获取系统中传递过来的MD5加密后的签名
        String hospSign = (String) paramMap.get("sign");
        //2 获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //3 根据医院编码查询数据库获取医院签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //5 判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除科室接口
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1 获取系统中传递过来的MD5加密后的签名
        String hospSign = (String) paramMap.get("sign");
        //2 获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //3 根据医院编码查询数据库获取医院签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //5 判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //科室编号
        String depcode = (String) paramMap.get("depcode");

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }


    //查询科室接口
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1 获取系统中传递过来的MD5加密后的签名
        String hospSign = (String) paramMap.get("sign");
        //2 获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //3 根据医院编码查询数据库获取医院签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //5 判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Integer page = 1;
        Integer limit = 10;
        if (ObjectUtils.isNotEmpty(paramMap.get("page"))) {
            page = Integer.parseInt(paramMap.get("page").toString());
        }
        if (ObjectUtils.isNotEmpty(paramMap.get("limit"))) {
            limit = Integer.parseInt(paramMap.get("limit").toString());
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service的方法
        Page<Department> page1 = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(page1);
    }

    //上传科室的接口
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1 获取系统中传递过来的MD5加密后的签名
        String hospSign = (String) paramMap.get("sign");
        //2 获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //3 根据医院编码查询数据库获取医院签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //5 判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service里面的方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    //查询医院的接口
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1 获取医院系统中传递过来的MD5加密后的签名
        String hospSign = (String) paramMap.get("sign");
        //2 获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //3 根据医院编码查询数据库获取医院签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //5 判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        System.out.println("-----保存医院信息-----");
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1 获取医院系统中传递过来的MD5加密后的签名
        String hospSign = (String) paramMap.get("sign");
        //2 获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //3 根据医院编码查询数据库获取医院签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //5 判断签名是否一致

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) paramMap.get("logoData");
        if (StringUtils.isNotBlank(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }

        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

}
